package com.ruoyi.campus.controller;

import com.ruoyi.campus.domain.SchoolInfo;
import com.ruoyi.campus.service.ISchoolInfoService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 小程序学校管理API控制器
 * 
 * @author campus-alarm
 * @date 2024-01-01
 */
@Anonymous
@RestController
@RequestMapping("/miniprogram/school")
public class MiniProgramSchoolController extends BaseController {

    @Autowired
    private ISchoolInfoService schoolInfoService;

    /**
     * 获取学校列表
     */
    @GetMapping("/list")
    public AjaxResult getSchoolList() {
        try {
            List<SchoolInfo> schools = schoolInfoService.selectSchoolInfoList(new SchoolInfo());
            return AjaxResult.success(schools);
        } catch (Exception e) {
            logger.error("获取学校列表失败", e);
            return AjaxResult.error("获取学校列表失败：" + e.getMessage());
        }
    }

    /**
     * 获取学校详情
     */
    @GetMapping("/detail/{id}")
    public AjaxResult getSchoolDetail(@PathVariable("id") Long schoolId) {
        try {
            SchoolInfo school = schoolInfoService.selectSchoolInfoBySchoolId(schoolId);
            if (school == null) {
                return AjaxResult.error("学校信息不存在");
            }
            return AjaxResult.success(school);
        } catch (Exception e) {
            logger.error("获取学校详情失败", e);
            return AjaxResult.error("获取学校详情失败：" + e.getMessage());
        }
    }

    /**
     * 获取班级列表
     */
    @GetMapping("/{id}/classes")
    public AjaxResult getClassList(@PathVariable("id") Long schoolId) {
        try {
            // TODO: 实现根据学校ID查询班级列表的逻辑
            List<Map<String, Object>> classes = new ArrayList<>();
            
            // 模拟数据
            for (int grade = 1; grade <= 6; grade++) {
                for (int classNum = 1; classNum <= 3; classNum++) {
                    Map<String, Object> classInfo = new HashMap<>();
                    classInfo.put("classId", grade * 10 + classNum);
                    classInfo.put("className", grade + "年级" + classNum + "班");
                    classInfo.put("gradeLevel", grade);
                    classInfo.put("studentCount", 30 + (int)(Math.random() * 20));
                    classes.add(classInfo);
                }
            }
            
            return AjaxResult.success(classes);
        } catch (Exception e) {
            logger.error("获取班级列表失败", e);
            return AjaxResult.error("获取班级列表失败：" + e.getMessage());
        }
    }
} 